06. Solution: Test Doubles

Solution: Test Doubles

ND079 JPND C3 L5 A04 Test Doubles Exercise Solution V2

Create a WorldTimeApiClient Interface

public interface WorldTimeApiClient {
    List<String> getValidTimeZones(String area);
}

Create a Fake implementation

public class FakeWorldTimeApiClient implements WorldTimeApiClient {

    private List<String> validTimeZones;
    public FakeWorldTimeApiClient(List<String> validTimeZones) {
        this.validTimeZones = validTimeZones;
    }

    @Override
    public List<String> getValidTimeZones(String area) {
        return validTimeZones;
    }
}

Write a Unit Test for getAvailableTimezoneText

class TimeZoneServiceTest {

    @Test
    void getAvailableTimezoneText_timeApiReturnsStringList_returnsCountriesAsString() {
        List<String> validTimeZones = List.of("Amsterdam", "Andorra", "Astrakhan", "Athens");
        TimeZoneService timeZoneService = new TimeZoneService(new FakeWorldTimeApiClient(validTimeZones));

        String expectedReturn = "Amsterdam, Andorra, Astrakhan, Athens";

        String timeZoneText = timeZoneService.getAvailableTimezoneText("Europe");

        assertTrue(timeZoneText.contains(expectedReturn));
    }
}